package yl.hs.bmipfull.controllers.finance;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.finance.IShipFreightSettleService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/*
 * ShipFreightSettleController
 * @category
 * @author 谢景广
 * @date 2022-08-03
 */
@Api(tags = "财务管理——发货运费结算")
@RequestMapping("/api/finance/shipFreightSettle")
@RestController
public class ShipFreightSettleController extends ControllerBase {
    @ApiOperation(value = "运费结算单查询数据接口", notes = "查询运费结算单")
    @RequestMapping(path = {"/{rowNum}","/{rowNum}/{page}"}, method = RequestMethod.POST)
    public WebResult querySettlePage(@RequestBody PageQuery query, @ApiParam("页面大小，为0时没有分页的临时数据，否则为正式结算单") @PathVariable int rowNum, @PathVariable(required = false) Integer page){
        try{
            var result = settleSvc.queryPage(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        }
        catch (Exception ee){
            return error(ee);
        }
    }
    @ApiOperation(value = "运费结算单详情数据接口", notes = "查询运费结算单明细和附加项")
    @RequestMapping(path = "/Details/{settleSN}", method = RequestMethod.GET)
    public WebResult findDetailsBySN(@ApiParam("运费结算单标识") @PathVariable String settleSN){
        try {
            var result = settleSvc.querySettleDetailsBySettleSN(settleSN);
            return success("ok",result);
        }
        catch (Exception ee){
            return error(ee);
        }
    }
    @ApiOperation(value = "运费结算单详情数据接口", notes = "查询运费结算单明细和附加项")
    @RequestMapping(path = "/Temp/{driverSN}/{productCateSN}/{startDate}/{endDate}", method = RequestMethod.GET)
    public WebResult findDetailsBySearch(@ApiParam("司机标识") @PathVariable String driverSN,
                                         @ApiParam("产品类别编号") @PathVariable String productCateSN,
                                         @ApiParam("发货开始日期") @PathVariable String startDate,
                                         @ApiParam("发货结束日期") @PathVariable String endDate){
        try{
            var result = settleSvc.queryTempSettleDetails(driverSN,productCateSN,startDate,endDate);
            return success("ok",result);
        }
        catch (Exception ee){
            return error(ee);
        }
    }

    @ApiOperation(value = "运费结算单取消审核并删除运费结算信息接口", notes = "运费结算单取消审核并删除运费结算信息")
    @RequestMapping(path = "/cancel/{settleSN}", method = RequestMethod.DELETE)
    public WebResult cancelSettle(@ApiParam("运费结算单标识") @PathVariable String settleSN){
        try{
            var result = settleSvc.cancelSettle(settleSN);
            if(result>0) {
                return success("ok");
            }
            else
            {
                return fail("取消失败");
            }
        }
        catch (Exception ee){
            return error(ee);
        }
    }

    @ApiOperation(value = "保存并审核运费结算单接口", notes = "保存并审核运费结算单接口")
    @RequestMapping(path = "/save", method = RequestMethod.POST)
    public WebResult saveSettle(@ApiParam(value = "提交的运费结算信息") @RequestBody SubmitModel submitModel){
        try
        {
            var result = settleSvc.saveSettle(submitModel);
            if (result>0){
                return success("审核成功");
            }
            else
            {
                return fail("审核失败");
            }
        }
        catch (Exception ee){
            return error(ee);
        }
    }

    @ApiOperation(value = "运费结算单详情数据接口", notes = "查询运费结算单明细和附加项")
    @RequestMapping(path = "/orders/{settleSN}", method = RequestMethod.GET)
    public WebResult querySettleOrders(@ApiParam("运费结算单标识") @PathVariable String settleSN){
        try{
            var result = settleSvc.querySettleOrdersBySettleSN(settleSN);
            return success("ok",result);
        }
        catch (Exception ee){
            return error(ee);
        }
    }


    @Autowired
    public void setSvSVC(ISearchViewService svc){
        this.svSVC = svc;
    }
    ISearchViewService svSVC;

    @Autowired
    public void setSettleSvc(IShipFreightSettleService svc){
        this.settleSvc = svc;
    }
    IShipFreightSettleService settleSvc;
}
